# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
import csv
import os

from itemadapter import ItemAdapter


class GuaziPipeline:
    def process_item(self, item, spider):
        # 判断是否存在download文件夹，如果没有则创建
        download_path = os.getcwd() + '/download/'  # 当前文件夹下的download文件夹
        if not os.path.exists(download_path):  # 判断文件夹或文件
            os.makedirs(download_path)
        with open(download_path + './瓜子二手车.csv', 'a', encoding='utf-8')as g:
            # 创建一个csv的DictWriter对象，这样才能够将写入csv格式数据到这个文件
            g_csv = csv.DictWriter(g, ['car_title', 'car_year', 'car_mileage', 'car_price', 'car_price_old'])
            # 写入多行行（当做数据）
            # info = ['movie_title', 'img_src', 'rating_num', 'people_num']
            # f_csv.writeheader()
            # item.pop("type")  # 删除type 这个key-value

            g_csv.writerows([item])
            print("保存信息到CSV....ok")
            return item
